/*******************************************
 *                                         *
 *******************************************/

#include "Timer.h"

namespace WindowsFramework
{
	Timer::Timer (void)
	{
		this->IsActive = false;
		this->FPS_String = new char[MAX_CHAR];
	}

	Timer::~Timer (void)
	{
		if (this->FPS_String)
			delete this->FPS_String;
	}

	void Timer::Start (void)
	{
		this->IsActive = true;
		this->CurTickCount = GetTickCount();
		this->Update();
	}

	void Timer::Update (void)
	{
		if (this->IsActive)
		{
			this->PrvTickCount = this->CurTickCount;
			this->CurTickCount = GetTickCount();
		}
	}

	void Timer::Stop (void)
	{
		this->IsActive = false;
	}

	float Timer::GetFrameTime (void)
	{
		this->FrameTime = (((float)this->CurTickCount - (float)this->PrvTickCount) / 1000.0f);
		return this->FrameTime;
	}

	float Timer::GetFPS (void)
	{
		this->FPS = this->GetFrameTime();
		if (this->FPS > 0)
			this->FPS = (1.0f / FPS);
		else
			this->FPS = 0.0f;
		return this->FPS;
	}

	char* Timer::GetFPS_String (void)
	{
		if (this->Converter.good())
		{
			this->Converter << this->GetFPS();
			strcpy_s(this->FPS_String, MAX_CHAR, this->Converter.str().c_str());
			this->Converter.str("");
		}
		else
			this->FPS_String = '\0';

		return this->FPS_String;
	}
};